<?php
ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);

require_once "../lib/WxPay.Api.php";
require_once '../lib/WxPay.Notify.php';
require_once 'log.php';
require '../../dbc.php';

//初始化日志
$logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);


class PayNotifyCallBack extends WxPayNotify
{
	//查询订单
	public function Queryorder($transaction_id)
	{
		$input = new WxPayOrderQuery();
		$input->SetTransaction_id($transaction_id);
		$result = WxPayApi::orderQuery($input);
		Log::DEBUG("query:" . json_encode($result));
		if(array_key_exists("return_code", $result)
			&& array_key_exists("result_code", $result)
			&& $result["return_code"] == "SUCCESS"
			&& $result["result_code"] == "SUCCESS")
		{
			return true;
		}
		return false;
	}
	
	//重写回调处理函数
	public function NotifyProcess($data, &$msg)
	{
		Log::DEBUG("call back:" . json_encode($data));
		$notfiyOutput = array();
		
		if(!array_key_exists("transaction_id", $data)){
			$msg = "输入参数不正确";
			return false;
		}
		
		//查询订单，判断订单真实性
		if(!$this->Queryorder($data["transaction_id"])){
			$msg = "订单查询失败";
			return false;
		}
		return true;
	}
}

Log::DEBUG("begin notify");
$notify = new PayNotifyCallBack();

$notify->Handle();
//$notify->Handle(false);

	//-------------------------另一个通知处理--------------------------------
	$postXml = $GLOBALS["HTTP_RAW_POST_DATA"]; //接收微信参数
	if (empty($postXml)) {
		return false;
	}
		
	    
	//数组转换成xml
	function arrayToXml($arr) {
		$xml = "<root>";
		foreach ($arr as $key => $val) {
			if (is_array($val)) {
				$xml .= "<" . $key . ">" . arrayToXml($val) . "</" . $key . ">";
			} else {
				$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
			}
		}
		$xml .= "</root>";
		return $xml;
	}
	
	
	//xml转换成数组
	function xmlToArray($xml) {
		libxml_disable_entity_loader(true);
		$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
		$val = json_decode(json_encode($xmlstring), true);
		return $val;
	}
	
	
	$attr = xmlToArray($postXml);
	
	$total_fee = $attr[total_fee];
	$open_id = $attr[openid];
	$out_trade_no = $attr[out_trade_no];
	$time = $attr[time_end];
	
	if($_REQUEST['return_code']=='SUCCESS'){
		$query="updata bld_dingdan set zhuangtai=1 where shdanhao='{$out_trade_no}'";
		$result=mysqli_query($dbc,$query)or die(mysqli_error($dbc).'\n'.$query);
	}
	
	//一行代码把数组变xml
	$xmlData=arrayToXml($_REQUEST);
	
	//发送数据到微信服务器,代表支付成功
	echo <<<str
	<xml>
	  <return_code><![CDATA[SUCCESS]]></return_code>
	  <return_msg></return_msg>
	</xml>
str;
	
	if($out_trade_no!='' && $_REQUEST['return_code']=='SUCCESS'){
		$query="updata bld_dingdan set zhuangtai=1 where shdanhao='{$out_trade_no}'";
		$result=mysqli_query($dbc,$query)or die(mysqli_error($dbc).'\n'.$query);
	}
	
	function jilu($arr){
		$str='';
		foreach($arr as $key=>$value)
		{
			if(is_array($value)){
				$str.=jilu($value);
			}else{		
				$str.=$key.'=>'.$value.'\t\r\n';
			}
		}
		return $str;
	}
	
	//写入支付结果到服务器文件
	$zhifujieguo=jilu($_REQUEST);
	
	$myfile = fopen("log.html", "a") or die("Unable to open file!");
	fwrite($myfile,'新支付通知纪录：'.date("Y-m-d H:i:s").'<hr>');
	fwrite($myfile, $zhifujieguo);
	
	fwrite($myfile, $xmlData);
	
	fclose($myfile);
?>